<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $verifyCode;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password,verifyCode', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'username'=>'帐号',
            'password'=>'密码',
            'verifyCode'=>'验证码',
			'rememberMe'=>'记住我',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate())
				$this->addError('password','帐号或密码错误!');
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
            		
			$session = yii::app()->session;
			$session[yii::app()->session->sessionID] = yii::app()->user->name;
			/**
			 * Fetching the user's login ip address
			 * NOTICE : This method is not fully tested.
			 * @author : xgqin <xgqin@guet.edu.cn>
			 * @date   : 12/16/2013
			 */
			$ip = "unknown";
			if (getenv('HTTP_CLIENT_IP') && 
				strcasecmp(getenv('HTTP_CLIENT_ID'), 'unknown')
			)
			{
				$ip = getenv('HTTP_CLIENT_IP');
			}
			elseif(getenv('HTTP_X_FORWARDED_FOR') &&
				strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')
			)
			{
				$ip = getenv('HTTP_X_FORWARDED_FOR');
			}
			elseif (getenv('REMOTE_ADDR') &&
				strcasecmp(getenv('REMOTE_ADDR'), 'unknown')
			)
			{
				$ip = getenv('REMOTE_ADDR');
			}
			elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&
				strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')
			)
			{
				$ip = $_SERVER['REMOTE_ADDR'];
			}

			/**
			 * Update the last_login_date, last_login_ip, login status for
			 * the current user
			 * @author : xgqin <xgqin@guet.edu.cn>
			 * @date   : 05/17/2013
			 */
			/** 
			 * I add the update of login(tbl_user) 
			 * @author : nonglei
			 * @date   : 24/12/2013
			 */

			User::model()->updateByPk($this->_identity->id, 
				array('last_login_date'=>new CDbExpression('NOW()'),
				'last_login_ip'=>$ip,
			));

			return true;
		}
		else
			return false;
	}
}
